<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit\Ctl;

use Srv\Apps\Proto\DTO\Skit\SkitChasingAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitChasingEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitChasingListDTO;
use Srv\Apps\Proto\Enum\PlatformEnum;
use Srv\Apps\Proto\VO\Skit\SkitChasingListVO;
use Srv\Apps\Proto\VO\Skit\SkitChasingVO;
use Srv\Apps\Proto\VO\Skit\SkitInfoVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class SkitChasingCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $skcfId
     * @return array
     * getSkitChasingSrc
     */
    public function getSkitChasingSrc(int $skcfId):array
    {
        if($skcfId < 1) return [];
        $isFail     = false;
        $info       = $this->DbAuto->getDataOne('SELECT  skcf_id, skcf_platform, skio_id, skcf_hot_play, skcf_sort, skcf_create_time, skcf_update_time FROM '.$this->DbAuto->getTableNameAll('skit_chasing_conf').' WHERE skcf_id='.$skcfId.' AND skcf_delete=0', $isFail);
        if($isFail || !isset($info['skcf_id'])) return [];
        return $info;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return SkitChasingListVO|null
     * getSkitChasingList
     */
    public function getSkitChasingList(Page $Page, array $whereData): ?SkitChasingListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE skcf_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case SkitChasingListDTO::_skcfId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skcf_id='. intval($val);
                    break;
                case SkitChasingListDTO::_skioId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_id='. intval($val);
                    break;
                case SkitChasingListDTO::_hotPlay:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skcf_hot_play='. intval($val);
                    break;
                case SkitChasingListDTO::_platform:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skcf_platform='. intval($val);
                    break;
            }
        }
        $isFail         = false;
        $sql            = 'SELECT SQL_CALC_FOUND_ROWS skcf_id, skcf_platform, skio_id, skcf_hot_play, skcf_sort, skcf_create_time, skcf_update_time FROM '.$this->DbAuto->getTableNameAll('skit_chasing_conf').$whereString.' ORDER BY skcf_id DESC '.$limitString;
        $list           = $this->DbAuto->getData($sql, $isFail);
        $totalNum       = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $SkitChasingVOList              = [];
        if(CommArray::isForeach($list)){
            $skioIdList                 = array_unique(array_map('intval', array_column($list, 'skio_id')));
            $SkitInfoVOList             = SkitInfoCtl::tag()->getSkitInfoBatch($skioIdList);
            foreach($list as $info){
                $skcfIdItem             = intval($info['skcf_id']);
                if($skcfIdItem < 1) continue;
                $SkitChasingVO          = $this->getSkitChasingVO($info);
                if(!($SkitChasingVO instanceof SkitChasingVO)) continue;
                $SkitInfoVO             = $SkitInfoVOList[intval($info['skio_id']??0)]??null;
                if($SkitInfoVO instanceof SkitInfoVO) $SkitChasingVO->setSkitInfo($SkitInfoVO);
                $SkitChasingVOList[]    = $SkitChasingVO;
            }
        }
        $SkitChasingListVO              = new SkitChasingListVO();
        $SkitChasingListVO->setList($SkitChasingVOList);
        $SkitChasingListVO->setPage($Page->getPageVO($totalNum));
        return $SkitChasingListVO;
    }

    /**
     * @param int $skcfId
     * @return SkitChasingVO|null
     * getSkitChasingInfo
     */
    public function getSkitChasingInfo(int $skcfId):?SkitChasingVO
    {
        $chasingSrc     = $this->getSkitChasingSrc($skcfId);
        if(!isset($chasingSrc['skcf_id'])) return null;
        $SkitChasingVO  = $this->getSkitChasingVO($chasingSrc);
        $SkitInfoVO     = SkitInfoCtl::tag()->getSkitInfoInfo(intval($chasingSrc['skio_id']??0));
        if($SkitInfoVO instanceof SkitInfoVO) $SkitChasingVO->setSkitInfo($SkitInfoVO);
        return $SkitChasingVO;
    }

    /**
     * @param array $info
     * @return SkitChasingVO|null
     * getSkitChasingVO
     */
    public function getSkitChasingVO(array $info):?SkitChasingVO
    {
        $skcfId             = intval($info['skcf_id']??0);
        if($skcfId < 1) return null;
        $SkitChasingVO      = new SkitChasingVO();
        $SkitChasingVO->setSkcfId($skcfId);
        $SkitChasingVO->setHotPlay(intval($info['skcf_hot_play']));
        $SkitChasingVO->setSort(intval($info['skcf_sort']));
        $SkitChasingVO->setCreateTime(intval($info['skcf_create_time']));
        $SkitChasingVO->setUpdateTime(intval($info['skcf_update_time']));
        $SkitChasingVO->setPlatform(PlatformEnum::parseValue($info['skcf_platform']));
        return $SkitChasingVO;
    }

    /**
     * @param array $setData
     * @return int
     * skitChasingAdd
     */
    public function skitChasingAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitChasingAddDTO::_skioId:
                    $skioId         = intval($val);
                    $skitInfoSrc    = SkitInfoCtl::tag()->getSkitInfoSrc($skioId);
                    if(!isset($skitInfoSrc['skio_id'])) return 0;
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_id='.$skioId;
                    break;
                case SkitChasingAddDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_sort='.intval($val);
                    break;
                case SkitChasingAddDTO::_hotPlay:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_hot_play='. intval($val);
                    break;
                case SkitChasingAddDTO::_platform:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_platform='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('skit_chasing_conf').' SET '.$setString.', skcf_delete=0, skcf_create_time='.$currTime.', skcf_update_time='.$currTime;
        $skcfId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $skcfId < 1) return 0;
        return $skcfId;
    }

    /**
     * @param int $skcfId
     * @param array $setData
     * @return bool
     * skitChasingEdit
     */
    public function skitChasingEdit(int $skcfId, array $setData):bool
    {
        if($skcfId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitChasingEditDTO::_skioId:
                    $skioId         = intval($val);
                    $skitInfoSrc    = SkitInfoCtl::tag()->getSkitInfoSrc($skioId);
                    if(!isset($skitInfoSrc['skio_id'])) return false;
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_id='.$skioId;
                    break;
                case SkitChasingEditDTO::_sort:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_sort='.intval($val);
                    break;
                case SkitChasingEditDTO::_hotPlay:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_hot_play='. intval($val);
                    break;
                case SkitChasingEditDTO::_platform:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcf_platform='. intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('skit_chasing_conf').' SET '.$setString.', skcf_update_time='.$currTime.' WHERE skcf_id='.$skcfId.' AND skcf_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }

    /**
     * @param int $skcfId
     * @return bool
     * skitChasingDel
     */
    public function skitChasingDel(int $skcfId):bool
    {
        if($skcfId < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp();
        $resNum     = $this->DbAuto->getDataNum('UPDATE '.$this->DbAuto->getTableNameAll('skit_chasing_conf').' SET skcf_delete=1, skcf_update_time='.$currTime.' WHERE skcf_id='.$skcfId.' AND skcf_delete=0', $isFail);
        if($isFail || $resNum < 1) return false;
        return true;
    }
}